<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('参股基金')" prop="dealId">
            <el-select v-model="form.dealId" :disabled="form.id === undefined ? false : true" filterable clearable :placeholder="$t('请选择')">
              <el-option
                v-for="item in dealNameOptions"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回款日期')" prop="moneyBackDate">
            <el-date-picker
              v-model="form.moneyBackDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('回款日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回款方式')" prop="moneyBackType">
            <el-select v-model="form.moneyBackType" filterable :placeholder="$t('回款方式')">
              <el-option
                v-for="dict in moneyBackTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本次退出本金') + $t('金额单位')" prop="quitCaptial">
            <Money v-model="form.quitCaptial" unit="w" :placeholder="$t('本次退出本金')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本次退出收益') + $t('金额单位')" prop="quitProfit">
            <Money v-model="form.quitProfit" unit="w" :placeholder="$t('本次退出收益')" />
          </el-form-item>
        </el-col>
        <!--<el-col :span="12">-->
        <!--  <el-form-item :label="$t('分红') + $t('金额单位')" prop="shareBonus">-->
        <!--    <Money v-model="form.shareBonus" unit="w" :placeholder="$t('分红')" />-->
        <!--  </el-form-item>-->
        <!--</el-col>-->
        <!--<el-col :span="12">-->
        <!--  <el-form-item :label="$t('其他') + $t('金额单位')" prop="other">-->
        <!--    <Money v-model="form.other" unit="w" :placeholder="$t('其他')" />-->
        <!--  </el-form-item>-->
        <!--</el-col>-->
        <el-col :span="12">
          <el-form-item :label="$t('本次退出总额') + $t('金额单位')" prop="quitTotalAmount">
            <Money v-model="form.quitTotalAmount" unit="w" :placeholder="$t('本次退出总额')" :disabled="true" />
          </el-form-item>
        </el-col>
        <!--<el-col :span="12">-->
        <!--  <el-form-item :label="$t('本次退出比例(%)')" prop="quitProportion">-->
        <!--    <Ratio v-model="form.quitProportion" :placeholder="$t('本次退出比例(%)')" />-->
        <!--  </el-form-item>-->
        <!--</el-col>-->
        <el-col :span="12">
          <el-form-item :label="$t('退出状态')" prop="quarter">
            <el-select v-model="form.exitStatus" value="" :placeholder="$t('退出状态')" @change="getLatestShareholdingRatio">
              <el-option
                v-for="dict in exitStatusOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.exitStatus === 'BFTC' || form.exitStatus === 'QBTC'" :span="12">
          <el-form-item :label="$t('最新持股比例(%)')" prop="latestShareholdingRatio">
            <Money v-model="form.latestShareholdingRatio" :placeholder="$t('最新持股比例(%)')" :disabled="form.exitStatus === 'QBTC'" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="company_project_moneyback" :business-id="getDealId" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="company:moneyback" />
    </div>
  </div>
</template>

<script>
    import { saveCompanyProjectMoneyback, getCompanyProjectMoneyback } from '@/api/company/moneyback/companyProjectMoneyback'
    import { getBfLatestShareholdingRatio, lisDealName } from '@/api/bf/investGroup/bfInvestGroup'
    export default {
        data() {
            return {
                moneyBackTypeOptions: [],
                exitStatusOptions: [],
                dealNameOptions: [],
                // 表单参数
                form: {
                  fileList: []
                },
                // 项目id
                getDealId: this.$route.query.id,
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  dealId: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  moneyBackDate: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  // moneyBackType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  quitTotalAmount: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
                // 查询公司指标参数
                queryParams: {
                  indirectDealId: this.companyId || this.$route.query.companyId,
                  source: 'BF_PORTFOLIO'
                }
            }
        },
        watch: {
          'form.quitCaptial'() {
            this.calcAmount()
          },
          'form.quitProfit'() {
            this.calcAmount()
          },
          'form.shareBonus'() {
            this.calcAmount()
          },
          'form.other'() {
            this.calcAmount()
          }
        },
        created() {
          this.getDicts('return_money_mode').then(response => {
            this.moneyBackTypeOptions = response.data
          })
          this.getDicts('quit_status').then(response => {
            this.exitStatusOptions = response.data
          })
          // 获取所有关联项目
          lisDealName(this.queryParams).then(response => {
            this.dealNameOptions = response.rows
          })
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
                if (data != null && data.id !== undefined) {
                  getCompanyProjectMoneyback(data.id).then(response => {
                    this.form = response.data
                  })
                  this.loading = false
                } else {
                  this.reset()
                  this.loading = false
                }
            },
            // 计算估值金额
            calcAmount() {
              this.form.quitTotalAmount = 0
              if (this.form.quitCaptial !== undefined && this.form.quitCaptial != null && this.form.quitCaptial !== '') {
                this.form.quitTotalAmount = parseFloat(this.form.quitCaptial)
              }
              if (this.form.quitProfit !== undefined && this.form.quitProfit != null && this.form.quitProfit !== '') {
                this.form.quitTotalAmount = parseFloat(this.form.quitTotalAmount) + parseFloat(this.form.quitProfit)
              }
              if (this.form.shareBonus !== undefined && this.form.shareBonus != null && this.form.shareBonus !== '') {
                this.form.quitTotalAmount = parseFloat(this.form.quitTotalAmount) + parseFloat(this.form.shareBonus)
              }
              if (this.form.other !== undefined && this.form.other !== null && this.form.other !== '') {
                this.form.quitTotalAmount = parseFloat(this.form.quitTotalAmount) + parseFloat(this.form.other)
              }
              if (this.form.quitTotalAmount === 0) {
                this.form.quitTotalAmount = undefined
              } else {
                this.form.quitTotalAmount = parseFloat(this.form.quitTotalAmount)
              }
            },
            // 获取最新持股比例字段
            getLatestShareholdingRatio(exitStatus) {
              if (exitStatus === 'QBTC') {
                this.form.latestShareholdingRatio = 0
              } else if (exitStatus === 'BFTC') {
                // 部分退出，获取参股基金的最新持股比例
                getBfLatestShareholdingRatio(this.form.dealId, this.form.companyId).then(response => {
                  this.$set(this.form, 'latestShareholdingRatio', response.data.latestShareholdingRatio)
                })
              } else {
                this.form.latestShareholdingRatio = undefined
              }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    companyId: this.$route.query.companyId,
                    dealId: this.$route.query.dealId,
                    moneyBackDate: undefined,
                    moneyBackType: undefined,
                    quitCaptial: undefined,
                    latestShareholdingRatio: undefined,
                    quitProfit: undefined,
                    shareBonus: undefined,
                    other: undefined,
                    quitTotalAmount: undefined,
                    quitProportion: undefined,
                    exitStatus: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined,
                    fileList: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveCompanyProjectMoneyback(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }

        }

    }
</script>
